package com.bbx.taxi.client.Task;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Handler;

import com.bbx.androidapi.util.ToastUtil;
import com.bbx.api.sdk.model.official.passanger.Officail_ComboBuild;
import com.bbx.api.sdk.model.official.passanger.Official_ORGChildsBuild;
import com.bbx.api.sdk.model.official.passanger.Official_ORGChildsBuild;
import com.bbx.api.sdk.model.official.passanger.Return.Officail_Combo;
import com.bbx.api.sdk.model.official.passanger.Return.Official_ORGChilds;
import com.bbx.api.sdk.model.official.passanger.Return.Official_ORGChilds;
import com.bbx.api.sdk.net.base.JsonBuild;
import com.bbx.api.sdk.net.official.passanger.Officail_ComboNet;
import com.bbx.api.sdk.net.official.passanger.Official_ORGNet;
import com.bbx.api.sdk.net.official.passanger.Official_ORGChildsNet;
import com.bbx.api.sdk.net.official.passanger.Official_StarNet;
import com.bbx.taxi.client.Bean.Value;
import com.bbx.taxi.client.Bean.Attribute.MainAttribute;
import com.bbx.taxi.client.Task.BaseAsyncTask;
import com.bbx.taxi.client.MyApplication;
import com.bbx.taxi.official.R;

/** 
 * @Path com.bbx.taxi.client.Task.MyORGChildsTask.java
 * @Author 严旭文
 * @Date 2016-3-31 上午11:17:04
 * @Company 蓝海（福建）信息技术有限公司
 * @Description
 * 获取子机构
 */
public class MyORGChildsTask{
	private Activity context;
	public static MyORGChildsTask instance;
	public int layer;
	public boolean automatic;
	public ORGChildsTask mORGChildsTask;
	public interface OnORGChildsListener {
		/**
		 *获取子机构
		 *automatic用于判断自动加载还是点击加载
		 */
		public void onORGChildsFinish(Official_ORGChilds mOfficial_ORGChilds,int layer,boolean automatic);
	}

	static OnORGChildsListener mOnORGChildsListener = null;

	public static void setOnORGChildsListener(OnORGChildsListener l) {
		mOnORGChildsListener = l;
	}
	/**
	 * @param context
	 * @param org_id
	 * @param layer  用于区分是第几层机构
	 * @return
	 */
	public static MyORGChildsTask getInstance(Activity context,String org_id,int layer,boolean automatic) {
		instance = new MyORGChildsTask(context,org_id,layer,automatic);
		return instance;
	}
	public MyORGChildsTask(Activity context,String org_id,int layer,boolean automatic) {
		this.layer=layer;
		this.automatic=automatic;
		this.context = context;
		getORGChilds(org_id);
	
	}
	/**
	 * 获取司机星星
	 */
	public void getORGChilds(String org_id) {
		Official_ORGChildsBuild mOfficial_ORGChildsBuild=new Official_ORGChildsBuild(context);
		mOfficial_ORGChildsBuild.opuid=MyApplication.getInstance().getUid();
		mOfficial_ORGChildsBuild.org_id=org_id;
		ORGChildsTask mORGChildsTask=new ORGChildsTask(context, mOfficial_ORGChildsBuild);
		mORGChildsTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
	}
	private class ORGChildsTask extends BaseAsyncTask {

		private Official_ORGChildsBuild mOfficial_ORGChildsBuild;

		public ORGChildsTask(Activity context, Official_ORGChildsBuild mOfficial_ORGChildsBuild) {
			super(context);
			this.mOfficial_ORGChildsBuild = mOfficial_ORGChildsBuild;
		}

		@Override
		public void onSuccess(Object object) {
			if (object != null) {
				Official_ORGChilds mOfficial_ORGChilds=(Official_ORGChilds)object;
				if(mOfficial_ORGChilds!=null){
					if (mOnORGChildsListener != null) {
						mOnORGChildsListener.onORGChildsFinish(mOfficial_ORGChilds,layer,automatic);
					}
				}else{
					ToastUtil.showToast(context, R.string.no_network);
					if (mOnORGChildsListener != null) {
						mOnORGChildsListener.onORGChildsFinish(null,layer,automatic);
					}
				}
				
			} else {
				ToastUtil.showToast(context, R.string.no_network);
				if (mOnORGChildsListener != null) {
					mOnORGChildsListener.onORGChildsFinish(null,layer,automatic);
				}
			}
		}

		@Override
		public void onFailed(int status, String msg) {
			ToastUtil.showToast(context, R.string.no_network);
			if (mOnORGChildsListener != null) {
				mOnORGChildsListener.onORGChildsFinish(null,layer,automatic);
			}
	
		}

		@Override
		protected Object doInBackground(Object... params) {
			// TODO Auto-generated method stub
			String json = new JsonBuild().setModel(mOfficial_ORGChildsBuild).getJson();
			return new Official_ORGChildsNet(context, json);
		}
	}

}
